<template>
  <div class="body" v-loading="loading">
    <div>
      <el-button type="info" size="small" icon="el-icon-back" @click="gotoList">&nbsp;返&nbsp;回&nbsp;</el-button>
    </div>
    <el-form ref="form" :model="header" size="small" label-width="80px" :inline="true" class="header">
      <div class="audit_icon iconfont icon-yishenhe" v-if="header.status === 'audit'"></div>
      <div class="header_row">
        <el-form-item style="width:260px;" label="入库编号">{{ header.billNo }}</el-form-item>
        <el-form-item style="width:260px;" label="合同编号">{{ header.HTBillNo }}</el-form-item>
        <el-form-item style="width:260px;" label="订单编号">{{ header.DDBillNo }}</el-form-item>
      </div>
      <div class="header_row">
        <el-form-item style="width:260px;" label="供应商编码">{{ header.supplierNo }}</el-form-item>
        <el-form-item style="width: 260px;" label="供应商名称">{{ header.supplierNo }}</el-form-item>
        <el-form-item style="width:260px;" label="仓库">{{ header.stockNo? header.stockNo + ":" + getStockName(header.stockNo): header.stockNo }}</el-form-item>
      </div>
      <div class="header_row">
        <el-form-item style="width:260px;" label="重量">{{ header.count }}</el-form-item>
        <el-form-item style="width:260px;" label="库管">{{ header.createUserName }}</el-form-item>
        <el-form-item style="width: 260px;" label="入库时间">{{ header.createTime }}</el-form-item>
      </div>
    </el-form>
    <el-table :data="dataList" class="detail" height="calc(100vh - 325px)">
      <el-table-column label="No." width="60" align="right" type="index"></el-table-column>
      <el-table-column label="物料编码" min-width="120" prop="materialNo"></el-table-column>
      <el-table-column label="物料名称" min-width="170" prop="materialName"></el-table-column>
      <el-table-column label="批次" width="150" prop="batchNo"></el-table-column>
      <el-table-column label="数量" width="100" prop="count" align="right"></el-table-column>
      <el-table-column label="金额" width="220" prop="settlementAmount" align="center">
        <template #default="scope">
          <template v-if="scope.row.settlementStatus === 'audit'">{{ scope.row.settlementAmount }}</template>
          <el-input-number size="small" v-model="scope.row.settlementAmount" :controls="false" style="width: 100%" v-else />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="90" fixed="right">
        <template #default="scope">
          <el-button type="text" style="color: var(--el-color-warning)" icon="el-icon-s-promotion" @click="unSettlementMaterial(scope.row)" v-if="scope.row.settlementStatus === 'audit'">反审</el-button>
          <el-button type="text" style="color: var(--el-color-primary)" icon="el-icon-s-promotion" @click="settlementMaterial(scope.row)" v-else>结算</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { getBoxNo } from '../../utils'
import BatchAddRowDialog from "../components/batchAddRowDialog.vue";

export default {
  components: {BatchAddRowDialog},
  data() {
    return {
      loading: false,
      stockList: [],
      header: {},
      dataList: []
    }
  },
  async mounted() {
    let res = await this.$api.getChildDictionaryListByKeys({
      keys: ['stock']
    });
    if (!res.res) return;
    this.stockList = res.stock;
    let billNo = this.$route.query.billNo
    if (billNo) {
      return this.getBill(billNo)
    } else {
      this.header.batchNo = dayjs().format('YYYYMMDDHHmmss')
    }
  },
  methods: {
    async settlementMaterial(row) {
      if (!row.settlementAmount) {
        this.$message.error('请输入结算金额!')
        return
      }
      this.loading = true
      try {
        let res = await this.callApi('settlement_material', { id: row.id, amount: row.settlementAmount })
        if (!res.res) return
        this.$message.success('结算完成')
        return this.getBill(this.header.billNo)
      } finally {
        this.loading = false
      }
    },
    async unSettlementMaterial(row) {
      this.loading = true
      try {
        let res = await this.callApi('un_settlement_material', { id: row.id })
        if (!res.res) return
        this.$message.success('结算反审完成')
        return this.getBill(this.header.billNo)
      } finally {
        this.loading = false
      }
    },
    async getBill(billNo) {
      let res = await this.callApi('get_pur_in_settlement_detail', { billNo })
      if (res.res) {
        this.dataList = res.data
        this.header = res.header
      }
    },
    /**
     * 获取仓库名
     */
    getStockName(val) {
      if (this.stockList && this.stockList.length > 0) {
        for(let i = 0; i < this.stockList.length; i++) {
          if (this.stockList[i].key === val) {
            return this.stockList[i].name;
          }
        }
      }
      return '';
    },
    gotoList() {
      return this.$router.push('/purchase/purSettlementOrderList')
    }
  }
}
</script>

<style lang="scss" scoped>
.body {
  padding: 20px 0;
  .header {
    margin-top: 20px;
    position: relative;
    .header_row {
      display: flex;

      :deep(.el-form-item__label) {
        width: 90px !important;
      }
      >div {
        flex-shrink: 0;
      }
      :deep(.produceDate) {
        .el-input {
          width: 100%!important;
        }
      }
    }
    .audit_icon {
      position: absolute;
      top: -10px;
      left: 60px;
      font-size: 48px;
      color: red;
    }
    .material {
      width: 100%;
    }
    .p3 {
      :deep(.el-form-item__content) {
        display: flex;
        .comment {
          margin-left: 10px;
          word-break: keep-all;
        }
      }
    }
  }
  .detail_header {
    display: flex;
    justify-content: space-between;
    margin-top: 10px;
    .stat {
      display: flex;
      font-size: 15px;
      >div {
        display: flex;
        align-items: baseline;
        line-height: 32px;
        .label {
          text-align: right;
          margin-right: 5px;
          color: #aaa;
        }
        .value {
          min-width: 50px;
          margin-right: 10px;
          color: #666;
        }
      }
    }
  }
}
</style>
